package com.kye.express.business.notice.notice_detail;

import android.os.Bundle;
import android.support.annotation.Nullable;

import com.kye.base.Logger;
import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.IView;
import com.kye.express.databinding.ActivityNoticeDetailBinding;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.notice.NotificationDetailBasicInfoBean;
import com.kye.model.bean.notice.request.NotificationDetailRequestParam;
import com.kye.model.datasource.NotificationDataSource;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 18/12/2017
 * @updated : 18/12/2017
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public interface NoticeDetailContract {

    interface NoticeDetailView extends IView {

        @Deprecated
        void showBottomSheetCommentInput();

        ActivityNoticeDetailBinding getDataBinding();

        long getNotificationId();

        void getNotificationDetailInfoSuccess(NotificationDetailBasicInfoBean notificationDetailBasicInfoBean);

        void getNotificationDetailInfoFailed(Throwable e);
    }

    class NoticeDetailPresenter extends BasePresenter<NoticeDetailView> {

        private final String TAG_NOTIFICATION_DETAIL_INFO_DATA = "TAG_NOTIFICATION_DETAIL_INFO_DATA";
        private final String TAG_NOTIFICATION_IS_INIT_REFRESH = "TAG_NOTIFICATION_IS_INIT_REFRESH";
        private NotificationDetailBasicInfoBean notificationDetailBasicInfoBean;
        private NotificationDataSource notificationDataSource;
        private boolean isInitRefresh = true; // todo : 记录是否

        public NoticeDetailPresenter() {
            notificationDataSource = new NotificationDataSource();
        }

        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            if (savedInstanceState != null) {
                isInitRefresh = savedInstanceState.getBoolean(TAG_NOTIFICATION_IS_INIT_REFRESH);
                notificationDetailBasicInfoBean = savedInstanceState.getParcelable(TAG_NOTIFICATION_DETAIL_INFO_DATA);
            }
        }

        @Override
        public void onSaveInstanceState(Bundle outState) {
            outState.putBoolean(TAG_NOTIFICATION_IS_INIT_REFRESH, isInitRefresh);
            outState.putParcelable(TAG_NOTIFICATION_DETAIL_INFO_DATA, notificationDetailBasicInfoBean);
        }

        /**
         * 获取通知的图文内容
         */
        public void getNotificationDetailInfo(long notificationId) {
            NotificationDetailRequestParam notificationDetailRequestParam = new NotificationDetailRequestParam();
            notificationDetailRequestParam.setNotificationId(notificationId);
            notificationDataSource
                    .getNotificationDetail(notificationDetailRequestParam)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<NotificationDetailBasicInfoBean>() {

                        @Override
                        public void onLoading(Disposable d) {
                            addDisposable(d);
                        }

                        @Override
                        public void onSuccess(OpenApiResult<NotificationDetailBasicInfoBean> result, NotificationDetailBasicInfoBean notificationDetailResutBean) {
                            if (isInitRefresh) {
                                isInitRefresh = false;
                            }
                            Logger.d("onSuccess:\n" + notificationDetailResutBean);
                            notificationDetailBasicInfoBean = notificationDetailResutBean;
                            getView().getNotificationDetailInfoSuccess(notificationDetailBasicInfoBean);
                        }

                        @Override
                        public void onApiError(OpenApiResult<NotificationDetailBasicInfoBean> result, int code, String msg) {
                            switch (code) {
                                case 36003:
                                    getView().getNotificationDetailInfoFailed(new RuntimeException(msg));
                                    break;
                                case 36001:
                                    getView().getNotificationDetailInfoFailed(new RuntimeException(msg));
                                    break;
                                default:
                                    getView().getNotificationDetailInfoFailed(new RuntimeException(msg));
                                    break;
                            }
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().getNotificationDetailInfoFailed(new RuntimeException("网络错误"));
                        }

                        @Override
                        public void onLoaded() {

                        }
                    });
        }

        public boolean isInitRefresh() {
            return isInitRefresh;
        }

        public void setInitRefresh(boolean initRefresh) {
            isInitRefresh = initRefresh;
        }
    }

}
